package com.kun.index.controller;


import com.kun.common.Constants;
import com.kun.common.ResultDto;
import com.kun.common.annotation.PermissionAuthentication;
import com.kun.index.entity.Blog;
import com.kun.index.entity.BlogLabel;
import com.kun.index.entity.Inform;
import com.kun.index.entity.Label;
import com.kun.index.service.impl.InformServiceImpl;
import com.kun.index.service.impl.UserServiceImpl;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 通知表 前端控制器
 * </p>
 *
 * @author kunkun
 * @since 2024-03-28
 */
@RestController
@RequestMapping("/index/inform")
public class InformController {

    @Autowired
    private InformServiceImpl informService;
    @Autowired
    private UserServiceImpl userService;

    @GetMapping("/queryAllInforms")
    @ApiOperation("查询所有通知")
    public ResultDto queryAllInforms(@RequestParam(name = "pageIndex", required=false)Integer pageIndex,
                                     @RequestParam(name = "queryTitleLike", required=false)String queryTitleLike,
                                     @RequestParam(name = "orderByCreateTime", required=false)String orderByCreateTime,
                                     @RequestParam(name = "pageSize", required=false)Integer pageSize){

        HashMap<String, Object> map = new HashMap<>();
        if (pageIndex != null) {
            map.put("pageIndex", pageIndex);
        }
        if (pageSize != null) {
            map.put("pageSize", pageSize);
        }
        if (queryTitleLike != null) {
            map.put("queryTitleLike", queryTitleLike);
        }
        if (orderByCreateTime != null) {
            map.put("orderByCreateTime", orderByCreateTime);
        }
        Map<String, Object> resultMap = informService.queryInforms(map);
        if (((List<Inform>) resultMap.get("informs")).size() > 0) {
            for (Inform inform : ((List<Inform>) resultMap.get("informs"))) {

//                对每个inform注入userNickname字段
                inform.setUserNickname(userService.queryUserById(inform.getUserId()).getNickname());

            }

            return ResultDto.success(resultMap);
        } else {
            return ResultDto.fail(Constants.CODE_301, "数据为空,请重新添加数据");
        }

    }

    @PostMapping("/addInform")
    @ApiOperation("新增通知")
    public ResultDto addInform(@RequestBody Inform inform){
        Integer id = informService.addInform(inform);
        if (id!=null){
            return ResultDto.success("新增通知成功");
        }else{
            return ResultDto.fail("新增通知失败");
        }
    }

    @PostMapping("/updateInform")
    @ApiOperation("编辑通知")
    public ResultDto updateInform(@RequestBody Inform inform){
        //更新博客信息
        int num = informService.updateInform(inform);

        if (num>0){
            return ResultDto.success("修改通知成功");
        }else{
            return ResultDto.fail("修改通知失败");
        }


    }

    @GetMapping("/queryInformById")
    @ApiOperation("通过id查询通知信息")
    public ResultDto queryInformById(@RequestParam(name = "id", required=true)Integer informId){
        Inform inform = informService.queryInformById(informId);
        if (inform!=null){
//            注入userNickname内容
            inform.setUserNickname(userService.queryUserById(inform.getUserId()).getNickname());


            return ResultDto.success(inform);
        }else{
            return ResultDto.fail(Constants.CODE_302,"数据查询不到,请重新查找数据");
        }


    }

    @PostMapping("/deleteInform")
    @ApiOperation("删除通知")
    public ResultDto deleteInform(@RequestBody Inform inform){
        int num = informService.deleteInform(inform);
        if (num>0){
            return ResultDto.success("删除通知成功");
        }else{
            return ResultDto.fail("删除通知失败");
        }
    }

    @PostMapping("/batchDeleteInform")
    @ApiOperation("批量删除通知")
    public ResultDto batchDeleteInform(@RequestBody List<Integer> ids){
        int num = informService.batchDeleteInform(ids);
        if (num>0){
            return ResultDto.success("删除通知成功");
        }else{
            return ResultDto.fail("删除通知失败");
        }


    }

    @GetMapping("/addHitsNum")
    @ApiOperation("新增通知点击量")
    public ResultDto addHitsNum(@RequestParam(name = "id", required=true)Integer id){
        Integer num = informService.addInformHitsNum(id);
        if (num!=null){
            return ResultDto.success(num);
        }else{
            return ResultDto.fail("增加通知点击量失败");
        }
    }




}

